<?php
namespace app\index\controller;
use think\facade\Db;
use app\BaseController;
use thans\jwt\facade\JWTAuth;

class Index extends BaseController
{
	public function index()
    {
        // 先要去config配置
        $param=[
            'title'=>'通知标题',
            'content'=>'通知内容',
            'path'=>'路径'
        ];

        $cid=0;

        // 引用uni push
        require_once("../extend/unipush/igetui.php");
        require_once("../extend/unipush/igetui/template/notify/IGt.Notify.php");
        
        // 全部
        if($cid === 0){
            /*
                普通通知 
            */
            $t = $param['title'];//推送通知的标题
            $c = $param['content'];//推送通知的内容
            $p = '';
            $l = 'http://www.baidu.com';
            $template = createNotiMessage($t, $c, $p);// 普通通知
            // $template = createLinkMessage($t, $c, $l);// 链接通知
            $message = new \IGtAppMessage();
            $message->set_data($template);//设置推送消息类型
            $message->set_isOffline(true);
            $message->set_offlineExpireTime(3600*1000);
            $message->set_AppIdList(array(APPID));
            /* 
                透传通知
            */
            // $payload = '';
            // if(isset($param['path']) && $param['path']){
            //     $payload = json_encode(['path'=>$param['path']]);
            // }
            // $p = json_encode(['title'=>$param['title'],'content'=>$param['content'],'payload'=>$payload]);
            // $template = createTranMessage($p);
            // //个推信息体
            // $message = new \IGtSingleMessage();
            // $message->set_data($template);//设置推送消息类型
            // $message->set_isOffline(true);//是否离线
            // $message->set_offlineExpireTime(3600*12*1000);//离线时间
            // $message->set_AppIdList(array(APPID));

            try {
                $res = $igt->pushMessageToApp($message);
            }catch(\Exception $e){
                return  json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            $payload = '';
            if(isset($param['path']) && $param['path']){
                $payload = json_encode(['path'=>$param['path']]);
            }
            $p = json_encode(['title'=>$param['title'],'content'=>$param['content'],'payload'=>$payload]);
            $template = createTranMessage($p);
            //个推信息体
            $message = new \IGtSingleMessage();
            $message->set_data($template);//设置推送消息类型
            $message->set_isOffline(true);//是否离线
            $message->set_offlineExpireTime(3600*12*1000);//离线时间
            // $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
            //接收方
            $target = new \IGtTarget();
            $target->set_appId(APPID);
            $target->set_clientId($cid);
            // $target->set_alias(Alias);
            try {
                $res = $igt->pushMessageToSingle($message, $target);
            }catch(\Exception $e){
                return  json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }

        if(isset($res['result']) && $res['result'] == 'ok'){
            return  json(['code'=>1,'msg'=>'消息推送成功']);
        }else{
            return  json(['code'=>0,'msg'=>'消息推送失败'. $res['result']]);
        }
        
        
    }

    public function getPushInfo(){
        require_once("../extend/unipush/igetui.php");
        dump($igt->getPushResult('OSA-0609_ckxNKtcl5y6zdYp34uUDQ6'));
    }

    public function getOnline(){
        require_once("../extend/unipush/igetui.php");
        dump($igt->getLast24HoursOnlineUserStatistics(APPID));
    }

    public function getUserStatus(){
        require_once("../extend/unipush/igetui.php");
        dump($igt->queryAlias(APPID,'c1249925fb6f42b3ca087aff68b1cd5f'));
        // dump($igt->bindAlias(APPID,'分组1','c1249925fb6f42b3ca087aff68b1cd5f'));
    }
}